<template>
    <a-card :bordered="false">
        <a-form ref="searchFormRef" name="advanced_search" :model="searchFormState" class="ant-advanced-search-form">
            <a-row :gutter="24">
                <a-col :span="6">
                    <a-form-item label="名称" name="name">
                        <a-input v-model:value="searchFormState.name" placeholder="请输入名称" />
                    </a-form-item>
                </a-col>
                <a-col :span="6">
                    <a-button type="primary" @click="tableRef.refresh(true)">查询</a-button>
                    <a-button style="margin: 0 8px" @click="reset">重置</a-button>
                </a-col>
            </a-row>
        </a-form>
        <s-table
            ref="tableRef"
            :columns="columns"
            :data="loadData"
            :alert="options.alert.show"
            bordered
            :row-key="(record) => record.id"
            :tool-config="toolConfig"
            :row-selection="options.rowSelection"
        >
            <template #operator class="table-operator">
                <a-space>
                    <a-button type="primary" @click="formRef.onOpen()" v-if="hasPerm('bizEquipmentTypeAdd')">
                        <template #icon><plus-outlined /></template>
                        新增
                    </a-button>
					<a-button type="primary" @click="() => uploadFormRef.openUpload()" v-if="hasPerm('bizEquipmentTypeUpload')">
						<UploadOutlined />
						导入
					</a-button>
					<a-button @click="exportBatchUserVerify" v-if="hasPerm('bizEquipmentTypeExport')">
						<template #icon><export-outlined /></template>
						{{ $t('user.batchExportButton') }}
					</a-button>
                    <xn-batch-delete
                        v-if="hasPerm('bizEquipmentTypeBatchDelete')"
                        :selectedRowKeys="selectedRowKeys"
                        @batchDelete="deleteBatchBizEquipmentType"
                    />
                </a-space>
            </template>
            <template #bodyCell="{ column, record }">
                <template v-if="column.dataIndex === 'action'">
                    <a-space>
                        <a @click="formRef.onOpen(record)" v-if="hasPerm('bizEquipmentTypeEdit')">编辑</a>
                        <a-divider type="vertical" v-if="hasPerm(['bizEquipmentTypeEdit', 'bizEquipmentTypeDelete'], 'and')" />
                        <a-popconfirm title="确定要删除吗？" @confirm="deleteBizEquipmentType(record)">
                            <a-button type="link" danger size="small" v-if="hasPerm('bizEquipmentTypeDelete')">删除</a-button>
                        </a-popconfirm>
                    </a-space>
                </template>
            </template>
        </s-table>
    </a-card>
	<uploadForm ref="uploadFormRef" @successful="tableRef.refresh(true)" />
    <Form ref="formRef" @successful="tableRef.refresh(true)" />
</template>

<script setup name="equipmenttype">
    import { cloneDeep } from 'lodash-es'
    import Form from './form.vue'
    import bizEquipmentTypeApi from '@/api/biz/bizEquipmentTypeApi'
	import downloadUtil from '@/utils/downloadUtil'
	import { message, Empty } from 'ant-design-vue'
	import uploadForm from "./uploadForm.vue";
    const searchFormState = ref({})
    const searchFormRef = ref()
    const tableRef = ref()
    const formRef = ref()
	const uploadFormRef = ref()
    const toolConfig = { refresh: true, height: true, columnSetting: true, striped: false }
    const columns = [
        {
            title: '名称',
            dataIndex: 'name'
        },
        {
            title: '排序',
            dataIndex: 'sortCode'
        },
    ]
    // 操作栏通过权限判断是否显示
    if (hasPerm(['bizEquipmentTypeEdit', 'bizEquipmentTypeDelete'])) {
        columns.push({
            title: '操作',
            dataIndex: 'action',
            align: 'center',
            width: '150px'
        })
    }
    const selectedRowKeys = ref([])
    // 列表选择配置
    const options = {
        // columns数字类型字段加入 needTotal: true 可以勾选自动算账
        alert: {
            show: true,
            clear: () => {
                selectedRowKeys.value = ref([])
            }
        },
        rowSelection: {
            onChange: (selectedRowKey, selectedRows) => {
                selectedRowKeys.value = selectedRowKey
            }
        }
    }
    const loadData = (parameter) => {
        const searchFormParam = cloneDeep(searchFormState.value)
        return bizEquipmentTypeApi.bizEquipmentTypePage(Object.assign(parameter, searchFormParam)).then((data) => {
            return data
        })
    }
    // 重置
    const reset = () => {
        searchFormRef.value.resetFields()
        tableRef.value.refresh(true)
    }
    // 删除
    const deleteBizEquipmentType = (record) => {
        let params = [
            {
                id: record.id
            }
        ]
        bizEquipmentTypeApi.bizEquipmentTypeDelete(params).then(() => {
            tableRef.value.refresh(true)
        })
    }
	// 批量导出校验并加参数
	const exportBatchUserVerify = () => {
		if ((selectedRowKeys.value.length < 1) && !searchFormState.value.searchKey ) {
			message.warning('请输入查询条件或勾选要导出的信息')
		}
		if (selectedRowKeys.value.length > 0) {
			const params = {
				ids: selectedRowKeys.value
					.map((m) => {
						return m
					})
					.join()
			}
			exportBatchUser(params)
			return
		}
		if (searchFormState.value.searchKey ) {
			const params = {
				searchKey: searchFormState.value.searchKey,
				// userStatus: searchFormState.value.userStatus
			}
			exportBatchUser(params)
		}
	}
	// 批量导出
	const exportBatchUser = (params) => {
		bizEquipmentTypeApi.bizEquipmentTypeExport(params).then((res) => {
			downloadUtil.resultDownload(res)
			tableRef.value.clearSelected()
		})
	}
    // 批量删除
    const deleteBatchBizEquipmentType = (params) => {
        bizEquipmentTypeApi.bizEquipmentTypeDelete(params).then(() => {
            tableRef.value.clearRefreshSelected()
        })
    }
</script>
